--- title: "L2-003 月饼" created: 2025-11-28 tags: - 算法 --- # L2-003 月饼 ## 题目 [L2-003 月饼](https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=994805071789801472&page=1) ![[image-3928a83f.png]] ## 思路分析 ![[image-b299c6eb.png]] ## 代码实现 ```cpp #include using namespace std; #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; const int inf = 0x3f3f3f3f; using PDD = pair; const int N=1010; double have[N]; double price[N]; priority_queue pie; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n,need;cin>>n>>need; for(int i=0;i> have[i]; } for(int i=0;i>tmp; price[i]=tmp*1.0/have[i]; } // for(int i=0;i0){ double cur_price = pie.top().first; double cur_have = pie.top().second; if(cur_have<=need){ sum+=cur_have * cur_price; need-=cur_have; }else if(cur_have>need){ sum+=need*cur_price; need=0; } pie.pop(); } printf("%.2f",sum); return 0; } ``` ## 同类题型 ## 视频讲解 --- ⬅️ [[L2-002 链表去重|L2-002 链表去重]] 🏠 [[00-天梯赛]] ➡️ [[L2-004 这是二叉搜索树吗?|L2-004 这是二叉搜索树吗?]]